<template>
	<view>
		<view style="padding: 0 30rpx;" v-if="orders">
			<u-form :model="form" ref="uForm" label-width="auto">
				<u-form-item label="项目名称" prop="item_name">
					<u-input  v-model="form.item_name" :disabled="true"  @click="show=true"/>
					<u-select v-model="show" value-name="charge_id" label-name="charge_name" :list="chargeList" @confirm="confirm"></u-select>
				</u-form-item>
				<u-form-item label="项目单价" prop="item_single_price">
					<u-input v-model="form.item_single_price" type="text" />
				</u-form-item>
				<u-form-item label="项目数量" prop="pay_item_usage" >
					<u-input v-model="form.pay_item_usage" type="number" />
				</u-form-item>
				<u-form-item label="项目来源" v-if="orders.orders_source_id!=1" >
					<u-radio-group v-model="form.pay_stage" @change="radioGroupChange">
						<u-radio v-for="(item, index) in [{value:1,name:'第三方'},{value:2,name:'商户'}]" :key="index" :name="item.value">
							{{item.name}}
						</u-radio>
					</u-radio-group>
				</u-form-item>
				<u-form-item label="计费方式" >
					<u-radio-group v-model="form.pay_item_check_type" @change="radioGroupChange">
						<u-radio v-for="(item, index) in [{id:1,name:'一次性收费',value:1},{id:2,name:'超时全额收费',value:2},{id:3,name:'超时部分收费',value:3}]" :key="index" :name="item.value">
							{{item.name}}
						</u-radio>
					</u-radio-group>
				</u-form-item>
				<u-form-item label="项目备注">
					<u-input v-model="form.item_name_remark" />
				</u-form-item>
		
				<u-form-item>
					<u-button type="primary" @click="submit">添加</u-button>
				</u-form-item>
			</u-form>
		</view>
		<u-toast ref="uToast"></u-toast>
		<!-- <u-avatar src="http://tmp/ExThmpgDoq9dd60ec45b54a0ac551cea6964878ba556.jpg"></u-avatar> -->
	</view>
</template>

<script>
	import {
		data
	} from 'uview-ui/libs/mixin/mixin';
	import cos from '../../utils/cos';
	// const COS = require('cos-js-sdk-v5');
	export default {
		data() {
			return {
				orders:null,
				show: false,
				host: '',
				chargeList: [],
				form: {
					item_name: '',
					item_single_price:'',
					pay_stage:1,
					pay_item_usage:'',
					pay_item_check_type:1,
					order_charge_id:'',
					order_charge_name:'',
					item_name_remark:''
					
				},
				rules: {
					item_name: [{
						required: true,
						message: '请输入名称',
						trigger: ['blur', 'change']
					}],
					item_single_price: [{
						required: true,
						message: '请输入单价',
						trigger: ['blur', 'change']
					}],
					pay_item_usage: [{
						required: true,
						message: '请输入数量',
						trigger: ['blur', 'change']
					}],
					pay_stage: [{
						required: true,
						message: '请选择项目来源',
						trigger: ['blur', 'change']
					}],
					pay_item_check_type: [{
						required: true,
						message: '请选择计费方式',
						trigger: ['blur', 'change']
					}]
					
				}
			}
		},
		onLoad(options) {
			this.orders = JSON.parse(decodeURIComponent(options.data));
			this.host = this.$http.host;
			console.log("onLoad");
			console.log(this.orders);
		},
		mounted() {
			this.getChargeList();
		},
		methods: {
			radioGroupChange(e){
				console.log(e);
			},
			confirm(e) {
				this.form.item_name=e[0].label;
				this.form.order_charge_name=e[0].label;
				this.form.order_charge_id=e[0].value;
				var item=this.chargeList.find((item)=>item.charge_id==e[0].value);
				if(item){
					this.form.pay_item_check_type=item.charge_check_type;
				}
				console.log(this.form);
				
			},
			getChargeList(){
				var that=this;
				that.$http.post('/orderCharge/ListByEntity', {}, (res) => {
					if (res.data.success) {
						that.chargeList=res.data.result.list;
					} else {
						that.refs.myModal.show('请求失败', res.data.result.message);
					}
				});
			},
			submit() {
				var that = this;
				var form=this.form;
				form.order_id = that.orders.orders_id;
				this.$refs.uForm.validate(valid => {
					if (valid) {
						that.$http.post('/orders/addPayAmount', form, (res) => {
							if (res.data.success) {
								that.$refs.uToast.show({
									title: '添加成功',
									type: "success"
								});
								uni.$emit("getOrdersInfo", that.orders);
								uni.navigateBack();
							} else {
								that.$refs.myModal.show('登录失败', res.data.result.message);
							}
						});
					} else {
						console.log('验证失败');
					}
				});
			}
		},
		onReady() {
			console.log("ready");
			this.$refs.uForm.setRules(this.rules);
		}
	}
</script>

<style>

</style>